package com.wn.class30.service.impl;

import com.wn.class30.entity.MealPay;
import com.wn.class30.entity.MealPayExample;
import com.wn.class30.mapper.MealPayDao;
import com.wn.class30.service.MealPayService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author yuyu
 * @since 2020/4/19 23:07
 */
@Service
public class MealPayServiceImpl implements MealPayService {
    @Resource
    private MealPayDao mealPayDao;

    @Override
    public long count(Map<String, String> map) throws Exception{
        return mealPayDao.countByExample(getMealPayExample(map));
    }

    @Override
    public List<MealPay> selectMealPays(Map<String, String> map) throws Exception {
        return mealPayDao.selectByExample(getMealPayExample(map));
    }

    public MealPayExample getMealPayExample(Map<String,String> map) throws Exception{
        MealPayExample mealPayExample = new MealPayExample();
        MealPayExample.Criteria criteria = mealPayExample.createCriteria();
        Long page = Long.parseLong(map.get("page"));
        Integer limit = Integer.parseInt(map.get("limit"));
        mealPayExample.setLimit(limit);
        mealPayExample.setOffset(page);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String startDate = map.get("startDate");
        String endDate = map.get("endDate");
        String usertel = map.get("usertel");
        String mealId = map.get("mealId");
        String mealMoney = map.get("mealMoney");
        if(!"".equals(usertel) && usertel != null ){
            criteria.andUsertelEqualTo(usertel);
        }
        if(!"".equals(mealId) && mealId != null){
            criteria.andMealIdEqualTo(Integer.parseInt(mealId));
        }
        if(!"".equals(mealMoney) && mealMoney != null){
            criteria.andMealMoneyEqualTo(Double.parseDouble(mealMoney));
        }
        if(!"".equals(startDate) && startDate != null){
            criteria.andPayTimeGreaterThanOrEqualTo(simpleDateFormat.parse(startDate));
        }

        if(!"".equals(endDate) && endDate != null){
            Date date = simpleDateFormat.parse(endDate);
            Calendar c = Calendar.getInstance();
            c.setTime(date);
            c.add(Calendar.DAY_OF_MONTH, 1);
            Date time = c.getTime();
            String format = simpleDateFormat.format(time);
            criteria.andPayTimeLessThanOrEqualTo(simpleDateFormat.parse(format));
        }
        mealPayExample.setOrderByClause("`pay_time` DESC");
        return mealPayExample;
    }
}
